package oop.core.skill;

import java.util.*;

import oop.core.item.EquippableItem;
import oop.core.map.*;

public abstract class ActiveSkill extends Skill implements Cloneable{
	protected long lifeTime;
	protected long hitCount;
	protected float effect;
	protected String image;
	protected long propogationDelay;
	protected int range;
	protected List<String> passiveSkillsThatAffectMe;
	protected List<String> weaponType;
	protected int cost;
	
	protected ActiveSkill(String name, int skillLevel, long lifeTime, long hitCount, float effect, String image, long delay, int range, List<String> passiveSkillsThatAffectMe, List<String> weaponType){
		super(name, skillLevel);
		this.lifeTime = lifeTime;
		this.hitCount = hitCount;
		this.effect = effect;
		this.image = image;
		this.propogationDelay = delay;
		this.range = range;
		this.passiveSkillsThatAffectMe = passiveSkillsThatAffectMe;
		this.weaponType = weaponType;
		cost = 5;
	}
	
	protected ActiveSkill(String name, int skillLevel, long lifeTime, long hitCount, float effect, String image, long delay, int range, List<String> passiveSkillsThatAffectMe, List<String> weaponType, int cost){
		super(name, skillLevel);
		this.lifeTime = lifeTime;
		this.hitCount = hitCount;
		this.effect = effect;
		this.image = image;
		this.propogationDelay = delay;
		this.range = range;
		this.passiveSkillsThatAffectMe = passiveSkillsThatAffectMe;
		this.weaponType = weaponType;
		this.cost = cost;
	}
	
	public List<String> getWeaponType(){
		return weaponType;
	}
	
	public List<String> getSkillsThatAffectMe(){
		return this.passiveSkillsThatAffectMe;
	}
	
	public boolean deriveSkill(List<PassiveSkill> skill, EquippableItem e){
		String weapon = null;
		if(e == null){
			weapon = "fists";
		}
		else{
			weapon = e.getEquipmentType();
		}
		boolean ret = false;
		if(weaponType != null)
		for(String s : weaponType)
			if(s.equals(weapon))
				ret = true;
		if(ret && skill != null){
			for(PassiveSkill ps : skill){
				String skillName = ps.getName();
				boolean isInList = false;
				for(String s : passiveSkillsThatAffectMe){
					if(s.equals(skillName))
						isInList = true;
				}
				if(isInList)
					updateSkill(ps);
			}
		}
		return true;
	}
	
	public void updateSkill(PassiveSkill ps){
		int level = ps.getSkillLevel();
		// optional for balance purposes
		level /= 2;
		// /optional
		for(int i = 0;i<level;i++)
			updateStats();
	}
	
	public void levelUp(){
		++this.skillLevel;
		updateStats();
	}
	
	public void updateStats(){
		++this.lifeTime;
		this.hitCount += 2;
		if(this.effect <= 0)
			this.effect -= 50;
		else
			this.effect += 50;
		this.propogationDelay -= 1;
		this.range++;
		// note: all of these values are random and are thus subject to change
	}
	
	public abstract List<TemporalLocation> calculateLocationSet(Location location, int direction);
	
	public ActiveSkill clone(){
		ActiveSkill cloned = (ActiveSkill)super.clone();
		cloned.lifeTime = lifeTime;
		cloned.hitCount = hitCount;
		cloned.effect = effect;
		cloned.image = image;
		cloned.propogationDelay = propogationDelay;
		cloned.range = range;
		cloned.passiveSkillsThatAffectMe = passiveSkillsThatAffectMe;
		cloned.weaponType = weaponType;
		return cloned;
	}

	public long getLifeTime() {
		return lifeTime;
	}

	public void setLifeTime(long lifeTime) {
		this.lifeTime = lifeTime;
	}

	public long getHitCount() {
		return hitCount;
	}

	public void setHitCount(long hitCount) {
		this.hitCount = hitCount;
	}

	public float getEffect() {
		return effect;
	}

	public void setEffect(float effect) {
		this.effect = effect;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public long getPropogationDelay() {
		return propogationDelay;
	}

	public void setPropogationDelay(long propogationDelay) {
		this.propogationDelay = propogationDelay;
	}

	public int getRange() {
		return range;
	}

	public void setRange(int range) {
		this.range = range;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Float.floatToIntBits(effect);
		result = prime * result + (int) (hitCount ^ (hitCount >>> 32));
		result = prime * result + ((image == null) ? 0 : image.hashCode());
		result = prime * result + (int) (lifeTime ^ (lifeTime >>> 32));
		result = prime * result
				+ (int) (propogationDelay ^ (propogationDelay >>> 32));
		result = prime * result + range;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		ActiveSkill other = (ActiveSkill) obj;
		if (Float.floatToIntBits(effect) != Float.floatToIntBits(other.effect))
			return false;
		if (hitCount != other.hitCount)
			return false;
		if (image == null) {
			if (other.image != null)
				return false;
		} else if (!image.equals(other.image))
			return false;
		if (lifeTime != other.lifeTime)
			return false;
		if (propogationDelay != other.propogationDelay)
			return false;
		if (range != other.range)
			return false;
		return true;
	}

	public int getCost() {
		return cost;
	}
}
